/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "构建渲染器节点.h"

#include <节点/渲染/渲染节点.h>
#include <节点/渲染/光栅渲染.h>



static void on_参数修改(S_Props& prop) {
	auto nodes = f_global_get激活节点();
	for (auto 节点 : nodes) {
		节点->f_set是否要更新(true);
	}
}

static void on_参数修改_更新属性面板(S_Props& prop) {
	auto nodes = f_global_get激活节点();
	for (auto 节点 : nodes) {
		节点->f_set是否要更新(true);

		for (auto& e : 节点->m_包含在多个视口的UI部件) {
			C_节点面板* 节点面板 = static_cast<C_节点面板*>(e);
			C_节点视口* view = static_cast<C_节点视口*>(节点面板->m_节点当前所在视口);
			节点面板->m_属性面板参数构建(view->m_属性栏, (S_结构指针)节点面板);
		}
	}
}



static void f_渲染器配置节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_渲染器配置节点* 节点 = dynamic_cast<C_渲染器配置节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	S_Props prop;

	props.push_back(节点->m_分辨率);
	props.push_back(节点->m_光反弹);
	props.push_back(节点->m_光采样);
	props.push_back(节点->m_采样);

	节点->m_瓦片渲染.m_UI->m_Update = on_参数修改_更新属性面板;
	props.push_back(节点->m_瓦片渲染);

	if (f_prop_Bool(节点->m_瓦片渲染)) {
		props.push_back(节点->m_瓦片大小);
	}


	节点->m_开启运动模糊.m_UI->m_Update = on_参数修改_更新属性面板;
	props.push_back(节点->m_开启运动模糊);

	if (f_prop_Bool(节点->m_开启运动模糊)) {
		节点->m_运动模糊采样.m_UI->m_Update = f_节点参数更新;
		props.push_back(节点->m_运动模糊采样);

		节点->m_运动模糊时间.m_UI->m_Update = f_节点参数更新;
		props.push_back(节点->m_运动模糊时间);
	}


	props.push_back(节点->m_输出图层);
	props.push_back(节点->m_渲染世界);

	节点->m_渲染世界.m_UI->m_Update = f_节点参数更新;
	props.push_back(节点->m_渲染世界);

	节点->m_场景遮罩.m_UI->m_Update = f_节点参数更新;
	props.push_back(节点->m_场景遮罩);

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_渲染器配置(S_UI渲染环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_渲染器配置节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_渲染器配置(plane, node);
	return plane;
}

void f_节点重置_渲染器配置(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_渲染器配置节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}




static void f_图像降噪节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_图像降噪节点* 节点 = dynamic_cast<C_图像降噪节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	S_Props prop;
	节点->m_加速计算方式.m_UI->m_Update = on_参数修改;
	props.push_back(节点->m_加速计算方式);

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_图像降噪(S_UI渲染环境& ctx, S_设备环境& ctx_3d, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_图像降噪节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_图像降噪(plane, node);
	return plane;
}

void f_节点重置_图像降噪(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_图像降噪节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}





static void f_渲染输出节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_渲染输出节点* node = dynamic_cast<C_渲染输出节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	S_Props prop;
	//node->m_颜色通道.m_UI->m_Update = on_参数修改;
	//props.push_back(node->m_颜色通道);

	node->m_格式.m_UI->m_Update = on_参数修改_更新属性面板;
	props.push_back(node->m_格式);

	if (f_prop_enum(node->m_格式) != 4) {
		node->m_位深.m_UI->m_Update = on_参数修改;
		props.push_back(node->m_位深);
	}

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_渲染输出(S_UI渲染环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_渲染输出节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_渲染输出(plane, node);
	return plane;
}

void f_节点重置_渲染输出(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_渲染输出节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}







static void f_光栅阴影渲染节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_光栅阴影渲染节点* node = dynamic_cast<C_光栅阴影渲染节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	//S_Props prop;
	//node->m_颜色通道.m_UI->m_Update = on_参数修改;
	//props.push_back(node->m_颜色通道);

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_光栅阴影渲染节点(S_UI渲染环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_光栅阴影渲染节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_光栅阴影渲染节点(plane, node);
	return plane;
}

void f_节点重置_光栅阴影渲染节点(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_光栅阴影渲染节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}





static void f_光栅环境渲染节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_环境渲染节点* node = dynamic_cast<C_环境渲染节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	S_Props prop;

	node->m_渲染分辨率.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_渲染分辨率);

	node->m_模糊.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_模糊);

	node->m_模糊采样.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_模糊采样);

	props.push_back(node->m_渲染纹理);

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_光栅环境渲染节点(S_UI渲染环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_环境渲染节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_光栅环境渲染节点(plane, node);
	return plane;
}

void f_节点重置_光栅环境渲染节点(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_光栅环境渲染节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}





static void f_光栅渲染节点_部件构建属性(C_属性栏* plane, S_结构指针 obj) {
	C_光栅渲染节点* node = dynamic_cast<C_光栅渲染节点*>(((C_节点面板*)obj)->m_NodeData);

	std::vector<S_Props> props;
	f_节点重构_重构属性面板数据((C_节点面板*)obj, props);

	S_Props prop;

	node->m_渲染分辨率.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_渲染分辨率);

	props.push_back(node->m_渲染纹理);

	node->m_AO深度偏移.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_AO深度偏移);

	node->m_AO半径.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_AO半径);

	node->m_AO强度.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_AO强度);

	node->m_AO厚度.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_AO厚度);

	node->m_AO采样.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_AO采样);



	node->m_阴影强度.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_阴影强度);

	node->m_阴影采样.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_阴影采样);



	node->m_环境亮度.m_UI->m_Update = f_节点参数更新;
	props.push_back(node->m_环境亮度);

	plane->f_构建属性部件(props);
}

C_节点面板* f_节点构建_光栅渲染节点(S_UI渲染环境& ctx, S_设备环境& dev_ctx, C_节点基类* node, C_节点树* tree, const std::u16string& name) {
	if (!node) {
		node = new C_光栅渲染节点(*S_节点框架::g_3D视口环境);
		node->m_ICO = u"注意";
	}
	C_节点面板* plane = new C_节点面板(node, ctx, E_节点区类型::e_节点区_物体);
	f_节点重置_光栅渲染节点(plane, node);
	return plane;
}

void f_节点重置_光栅渲染节点(C_节点面板* plane, C_节点基类* node) {
	f_节点重构_面板插座重构(plane, node, E_节点区类型::e_节点区_物体);
	plane->m_属性面板参数构建 = f_光栅渲染节点_部件构建属性;
	plane->m_ICO = node->m_ICO;
}







void f_渲染节点_构建函数初始化() {
	C_节点面板::g节点面板构建函数[DEF_图像降噪节点] = f_节点构建_图像降噪;
	C_节点面板::g节点面板重置函数[DEF_图像降噪节点] = f_节点重置_图像降噪;
	C_节点面板::g节点构建函数_创建名称对应类型名称[u"降噪"] = DEF_图像降噪节点;

	C_节点面板::g节点面板构建函数[DEF_光栅阴影渲染节点] = f_节点构建_光栅阴影渲染节点;
	C_节点面板::g节点面板重置函数[DEF_光栅阴影渲染节点] = f_节点重置_光栅阴影渲染节点;
	C_节点面板::g节点构建函数_创建名称对应类型名称[u"光栅阴影"] = DEF_光栅阴影渲染节点;

	C_节点面板::g节点面板构建函数[DEF_光栅环境渲染节点] = f_节点构建_光栅环境渲染节点;
	C_节点面板::g节点面板重置函数[DEF_光栅环境渲染节点] = f_节点重置_光栅环境渲染节点;
	C_节点面板::g节点构建函数_创建名称对应类型名称[u"光栅环境"] = DEF_光栅环境渲染节点;

	C_节点面板::g节点面板构建函数[DEF_光栅渲染节点] = f_节点构建_光栅渲染节点;
	C_节点面板::g节点面板重置函数[DEF_光栅渲染节点] = f_节点重置_光栅渲染节点;
	C_节点面板::g节点构建函数_创建名称对应类型名称[u"光栅渲染"] = DEF_光栅渲染节点;

}





